package com.ykj.service.impl;

import com.ykj.dto.Result;
import com.ykj.entity.Collect;
import com.ykj.mapper.CollectMapper;
import com.ykj.service.CollectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ykj.utils.MyUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Y2014188432
 * @since 2022-05-17
 */
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements CollectService {

    @Override
    public Result addByStateAndTypeId(String state, String typeId) {
        // 1 创建 Collect 对象
        Collect collect = new Collect();
        collect.setUserId(Integer.valueOf(MyUtils.get()));
        collect.setState("1");
        // 2 若 state = 1 ,对象为板块
        if("1".equals(state)){
            collect.setPlateId(Integer.valueOf(typeId));
            save(collect);
            return Result.ok();
        }
        if("2".equals(state)){
            collect.setPostsId(Integer.valueOf(typeId));
            save(collect);
            return Result.ok();
        }
        return Result.fail("未知错误");
    }

    @Override
    public Result getIfCollectByUserIdAndStateAndTypeId(String state, String userId, String typeId) {
        userId = MyUtils.get();
        if("1".equals(state)){
            Collect collect = query()
                    .eq("user_id", userId)
                    .eq("plate_id", typeId)
                    .eq("state", "1").one();
            if (collect == null) {
                return Result.fail("error");
            }
            return Result.ok();
        }
        if("2".equals(state)){
            Collect collect = query()
                    .eq("user_id", userId)
                    .eq("posts_id", typeId)
                    .eq("state", "1").one();
            if (collect == null) {
                return Result.fail("error");
            }
            return Result.ok();
        }
        return Result.fail("error");
    }
}
